﻿using System.Collections.Generic;

namespace HCore
{
    public class TurnData
    {
        private int _playerId;
        public int checkSum;
        public int randomValue;
        private int _turnCount;
        private readonly List<Command> _commands = new List<Command>();

        public TurnData()
        {
        }

        public TurnData(int playerId, int turnCount)
        {
            _turnCount = turnCount;
            _playerId = playerId;
        }

        public void AddCommand(Command command)
        {
            _commands.Add(command);
        }

        public void ProcessCommands(HGame game)
        {
            foreach (var command in _commands)
            {
                command.ProcessCommand(game);
            }
        }


        public void Read(InputMemoryBitStream input)
        {
            _playerId = input.ReadInt();
            checkSum = input.ReadInt();
            randomValue = input.ReadInt();
            _turnCount = input.ReadInt();
            int count = input.ReadInt();
            for (int i = 0; i < count; i++)
            {
                _commands.Add(CommandFactory.ReadAndCreate(input));
            }
        }

        public void Write(OutputMemeryBitStream output)
        {
            output.WriteInt(_playerId);
            output.WriteInt(checkSum);
            output.WriteInt(randomValue);
            output.WriteInt(_turnCount);
            output.WriteInt(_commands.Count);
            foreach (var command in _commands)
            {
                command.Write(output);
            }
        }

        public bool Empty()
        {
            return _playerId == 0;
        }

        public int turnCount => _turnCount;

        public int playerId => _playerId;
    }
}